package com.yanqu.road.server.manger.activity.ghost;

import com.yanqu.road.entity.activity.ghost.GhostSimplePlayer;
import com.yanqu.road.entity.activity.ghost.config.GhostConfig;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.GhostPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostSceneLogic {

    private static final Object lockRoomObject = new Object();

    // 各boss房间内玩家信息   ：  Map<BossId，List<用户Id>>
    private static Map<Integer, List<Long>> bossRoomMap = new ConcurrentHashMap<>();

    // 各玩家所在Boss房间  ：   Map<用户Id，BossId>
    private static Map<Long, Integer> userRoomMap = new ConcurrentHashMap<>();

    /**
     * 初始化房间
     */
    private static void initRoom(int bossId) {
        if (!bossRoomMap.containsKey(bossId)) {
            bossRoomMap.put(bossId, new LinkedList<>());
        }
    }

    /**
     * 进入房间
     */
    public static void enterRoom(GamePlayer player, int bossId) {
        synchronized (lockRoomObject) {
            initRoom(bossId);
            // 旧房间移除
            Integer oldBossId = userRoomMap.get(player.getUserId());
            if (oldBossId != null) {
                bossRoomMap.get(oldBossId).remove(player.getUserId());
            }
            // 加新房间
            userRoomMap.put(player.getUserId(), bossId);
            bossRoomMap.get(bossId).add(player.getUserId());
        }
    }

    /**
     * 退出房间
     */
    public static void quitRoom(long userId) {

        // 判断有没有活动
        GhostConfig ghostConfig = GhostMgr.getGhostConfig();
        if (ghostConfig == null || !ActivityMgr.activityInTime(ghostConfig.getActivityInfo())) {
            return;
        }

        // 本服内移除
        Integer oldBossId = userRoomMap.get(userId);
        if (oldBossId != null) {
            synchronized (lockRoomObject) {
                userRoomMap.remove(userId);
                bossRoomMap.get(oldBossId).remove(userId);
            }
        }

        int activityId = GhostMgr.getActivityId();
        if (activityId <= 0) {
            return;
        }
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId());
        if (userBaseInfo == null) {
            return;
        }

        // 跨服移除
        GhostSimplePlayer simplePlayer = new GhostSimplePlayer();
        simplePlayer.setUserId(userId);
        simplePlayer.setServerId(GameServer.getInstance().getServerId());
        simplePlayer.setNickName(userBaseInfo.getNickName());
        simplePlayer.setDecoration(userBaseInfo.getDecoration());

        GhostActivityProto.CrossQuitBossRoomReqMsg.Builder builder = GhostActivityProto.CrossQuitBossRoomReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setPlayerData(GhostPb.parseGhostSimplePlayerMsg(simplePlayer));
        GamePlayerMgr.sendPacket(userId, YanQuMessageUtils.buildMessage(CrossProtocol.C2_GHOST_ACTIVITY_QUIT_BOSS_ROOM, builder));
    }


    /**
     * 获取房间内玩家
     */
    public static List<Long> getRoomUserList(int bossId) {
        if (!bossRoomMap.containsKey(bossId)) {
            return new ArrayList<>();
        }
        return new ArrayList<>(bossRoomMap.get(bossId));
    }

}
